<template lang="">
    <div>
        <el-dialog :title="title" :visible.sync="open" width="80%">
            <el-row :style="{ marginBottom: '10px' }">
                <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd">创建临时指令任务</el-button>
                <el-button type="warning" icon="el-icon-plus" size="mini" @click="handleExecute">下发子任务</el-button>
                <el-button type="success" icon="el-icon-refresh" size="mini" @click="handleRefresh" style="float: right">手动刷新子任务</el-button>
            </el-row>

            <el-table :data="list" border>
                <el-table-column prop="receiveUserName" label="执行人" width="180"></el-table-column>
                <el-table-column prop="taskDeptName" label="所属部门" width="180"></el-table-column>
                <el-table-column label="任务内容" align="left" prop="taskContent" />
                <el-table-column label="辅助信息" align="left" prop="taskContent" />
                <el-table-column label="任务状态" align="center" prop="drillStatus">
                    <template slot-scope="scope">
                        <el-tag type="warning" v-if="scope.row.status == 0" size="mini">待下发</el-tag>
                        <el-tag type="primary" v-if="scope.row.status == 1" size="mini">执行中</el-tag>
                        <el-tag type="success" v-if="scope.row.status == 2" size="mini">已完成</el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="250">
                    <template slot-scope="scope">
                        <el-button @click="handleTaskEdit(scope.row)" v-if="scope.row.status < 2" type="text" size="small">编辑</el-button>
                        <el-button @click="handleTaskDel(scope.row)" v-if="scope.row.status == 0" type="text" size="small">删除</el-button>
                        <el-button @click="handleTaskDetail(scope.row)" type="text" size="small">任务详情</el-button>
                        <el-button size="mini" type="text" v-if="scope.row.receiveUser != null" @click="chat(scope.row)">任务沟通</el-button>
                        <el-button type="text" size="mini" v-if="scope.row.receiveUser != null" @click="handleTaskNotice(scope.row)">任务提醒</el-button>
                    </template>
                </el-table-column>
            </el-table>

            <el-row :style="{ textAlign: 'center', marginTop: '15px' }">
                <el-button type="primary" icon="el-icon-check" size="mini" @click="handleComplete">完成任务</el-button>
            </el-row>
        </el-dialog>
        <handle-task-edit ref="handleTaskEdit" @handleOk="onHandleTaskSave" />
        <ChatIM ref="chatIM" />
        <TaskWarn ref="taskWarnRef" />
        <TaskSonDetail ref="taskDetailRef" />
    </div>
</template>
<script>
/**
 * 查询演练子任务列表弹窗
 * 用于查询一级任务下属的所有二级子任务
 * 功能用于任务下发或者添加临时任务
 */
import dialogMixin from "@/mixins/dialogMixin";
import * as HandleApi from "@/api/response/handle";
import HandleTaskEdit from "./HandleTaskEdit.vue";
import { editDeptTask, addDeptTask, openSecondTasks, completeTask } from "@/api/response/task";
import chatMixin from "@/mixins/chatMixin";
import ChatIM from "@/components/ChatIM/task.vue";
import TaskWarn from "./TaskWarn.vue";
import TaskSonDetail from "./HandleSonTaskDetail.vue";

export default {
    name: "HandleSonTasks",
    mixins: [dialogMixin, chatMixin],
    components: {
        HandleTaskEdit,
        ChatIM,
        TaskWarn,
        TaskSonDetail,
    },
    data() {
        return {
            title: "救援子任务列表",
            list: [],
            taskId: 0,
            handleId: 0,
            levelId: 0,
        };
    },
    mounted() {},
    methods: {
        showList(taskId, handleId, levelId) {
            console.log(taskId, handleId, levelId);
            this.taskId = taskId;
            this.handleId = handleId;
            this.levelId = levelId;
            this.handleRefresh();
            this.show();
        },
        handleRefresh() {
            HandleApi.getHandleSonTaskList(this.taskId).then(res => {
                this.list = res.data;
            });
        },
        handleAdd() {
            this.$refs.handleTaskEdit.addChildTask();
        },
        handleTaskEdit(row) {
            this.$refs.handleTaskEdit.edit(row);
        },
        onHandleTaskSave(data) {
            if (data.id == 0) {
                addDeptTask({
                    handleId: this.handleId,
                    nodeId: this.levelId,
                    parentId: this.taskId,
                    receiveUser: data.receiveUser,
                    receiveUserName: data.receiveUserName,
                    taskDept: data.taskDept,
                    taskDeptName: data.taskDeptName,
                    taskContent: data.taskContent,
                    helpContent: data.helpContent,
                    attachIds: data.attachIds,
                }).then(res => {
                    this.$modal.msgSuccess("任务添加成功");
                    this.$refs.handleTaskEdit.close();
                    this.handleRefresh();
                });
            } else {
                editDeptTask({
                    id: data.id,
                    receiveUser: data.receiveUser,
                    receiveUserName: data.receiveUserName,
                    taskDept: data.taskDept,
                    taskDeptName: data.taskDeptName,
                    taskContent: data.taskContent,
                    helpContent: data.helpContent,
                    attachIds: data.attachIds,
                }).then(res => {
                    if (res.code == 200) {
                        this.$modal.msgSuccess("任务修改成功");
                        this.$refs.handleTaskEdit.close();
                        this.handleRefresh();
                    } else {
                        this.$modal.msgError("任务修改失败");
                    }
                });
            }
        },
        handleExecute() {
            // 判断子任务的receiveUser是否都不为空
            if (this.list.some(item => item.receiveUser == null)) {
                this.$modal.msgError("请先为子任务选择执行人员");
                return;
            }

            openSecondTasks(this.taskId).then(res => {
                if (res.code == 200) {
                    // 开启成功
                    this.$modal.msgSuccess("子任务已经下发");
                    this.handleRefresh();
                } else {
                    // 开启失败
                    this.$modal.msgError("子任务下发失败");
                }
            });
        },
        handleComplete() {
            // 二级保障部门检查所有子任务都完成后，向总指挥提交任务完成
            if (this.list.some(item => item.status < 2)) {
                this.$modal.msgError("请确保所有任务均已完成");
                return;
            }
            completeTask(this.taskId).then(res => {
                if (res.code == 200) {
                    // 提交成功
                    this.$modal.msgSuccess("任务已完成");
                    this.$emit("handleOk");
                    this.close();
                } else {
                    // 提交失败
                    this.$modal.msgError("任务提交失败");
                }
            });
        },
        handleTaskDel() {},
        chat(row) {
            this.initTaskChat("handle", row.handleId, row.id, row.receiveUserName);
        },
        handleTaskNotice(row) {
            this.$refs.taskWarnRef.showDialog(row);
        },
        handleTaskDetail(row) {
            this.$refs.taskDetailRef.showDialog(row);
        },
    },
};
</script>
<style lang=""></style>
